package com.gwc.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(
        urlPatterns = {"/httpServletResponse"}
)
public class HttpServletResponse extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws ServletException, IOException {
        // 设置相应行
        resp.setStatus(200);

        // 设置响应头
        resp.setHeader("aaa","test");
        resp.setHeader("Content-Type","text/html");
//        resp.setContentType();
        resp.setHeader("Content-Length","1234");
//        resp.setContentLength();
        // 设置响应字符集,服务器去适应客户端，不太推荐
//        resp.setCharacterEncoding("utf-8");
        // 主动告知客户端编码方式，让客户端适应服务器
        resp.setContentType("text/html;charset=UTF-8");

        // 设置响应体
        PrintWriter writer = resp.getWriter();
        writer.write("<h1>噶沃尔</h1>");
    }
}
